package Test;

import basic.Test;

import java.util.*;

/**
 * @author Kevin
 * @date 2020/3/31 20:02
 */
public class example {

    public static String toBinaryString(String line) {
        StringBuffer buffer = new StringBuffer();
        long l = Long.parseLong(line);
        while(l != 0) {
            buffer.append(l % 2);
            l = l / 2;
        }
        return buffer.toString();
    }

    public static List<List<Integer>> Find3th(int[] list, int num) {
        if (list.length < 3) return null;
        Arrays.sort(list);

        List<List<Integer>> result = new ArrayList<>();
        int sum;
        for(int k = 0; k < list.length -2; k++) {
            int l = k + 1;
            int r = list.length -1;
            sum = num - list[k];

            while(l < r) {
                if (list[l] + list[r] == sum) {
                    result.add(Arrays.asList(list[k], list[l], list[r]));
                    while (l < r && list[l+1] == list[l]) l++;//去除重复值
                    while (l < r && list[r-1] == list[r]) r--;
                    l++;
                    r--;
                } else if(list[l] + list[r] < sum) {
                    l++;
                } else {
                    r--;
                }
            }
        }

        return result;
    }


    public static void main(String[] args) {
        //System.out.println(toBinaryString("5"));
        int[] a = new int[]{1,-2,0,5,-1,-4};

        List<List<Integer>> list = Find3th(a, 2);
        System.out.println(list);
    }
}
